package com.bookocean.infrastructure.persistence.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bookocean.infrastructure.persistence.po.BookCommentPO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 书摘评论 Mapper 接口
 * 
 * @author BookOcean
 * @since 2024-01-01
 */
@Mapper
public interface BookCommentMapper extends BaseMapper<BookCommentPO> {

    /**
     * 分页查询评论（按时间倒序）
     * 
     * @param targetType 目标类型
     * @param targetId 目标ID
     * @param offset 偏移量
     * @param pageSize 每页数量
     * @return 评论列表
     */
    @Select("SELECT * FROM comment_po WHERE target_type = #{targetType} AND target_id = #{targetId} AND parent_id IS NULL " +
            "ORDER BY created_at DESC LIMIT #{offset}, #{pageSize}")
    List<BookCommentPO> selectByTargetWithPaging(@Param("targetType") Integer targetType,
                                                 @Param("targetId") Long targetId, 
                                                 @Param("offset") Integer offset, 
                                                 @Param("pageSize") Integer pageSize);

    /**
     * 统计评论总数
     * 
     * @param targetType 目标类型
     * @param targetId 目标ID
     * @return 评论总数
     */
    @Select("SELECT COUNT(*) FROM comment_po WHERE target_type = #{targetType} AND target_id = #{targetId} AND parent_id IS NULL")
    Long countByTarget(@Param("targetType") Integer targetType, @Param("targetId") Long targetId);

    /**
     * 查询评论的回复列表
     * 
     * @param parentId 父评论ID
     * @return 回复列表
     */
    @Select("SELECT * FROM comment_po WHERE parent_id = #{parentId} ORDER BY created_at ASC")
    List<BookCommentPO> selectRepliesByParentId(@Param("parentId") Long parentId);
}
